package com.qingke.servlets;

import java.io.IOException;
import java.util.Enumeration;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class ContextTest
 */
@WebServlet("/ContextTest")
public class ContextTest extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ContextTest() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("测试: ").append(request.getContextPath()+"\r\n");
		//Context 整个项目的全局变量，在web文件中配置，在该项目下任何一个servlet中都拿到该项目下的context
		//得到context
		ServletContext sc = this.getServletContext();
//		sc.setInitParameter("para3", "value3");
		sc.setAttribute("para3", "value3");
		//获取所有context中的name，通过name来get到value
		Enumeration<String> names = sc.getInitParameterNames();
		while(names.hasMoreElements()){
			String name = names.nextElement();
			String value = sc.getInitParameter(name).toString();
			response.getWriter().append(name+"---"+value+"\r\n");
		}
		sc.removeAttribute("para3");
	
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
